import { EnumBjlResult } from "@/models/enums/EnumBjlResult";
import { ExcutePlan, RecmInfo, RoundInfo } from "../ExecutePlan";
import { FixedStrategy } from "../recmStrategy/FixedStrategy";
import { IMoneyStrategy } from "../IMoneyStrategy";
import { IPlan } from "../IExecutePlan";
import { GroupStepAddMoneyStrategy, NormalGroupMoneyStrategy } from "./NormalGroupMoneyStrategy";


export class WisdomMoneyStrategyV4 implements IMoneyStrategy {
    subPlan: IPlan[];
    debugCompoenent: string = "WisdomMoneyStrategyV3Debug";
    strategyName: string;
    startIndex: number = -1;
    startLevel: number = 500;
    constructor(name?: string) {
        this.strategyName = name ?? "GroupMoneyStrategy" + (new Date()).getMilliseconds() + parseInt((Math.random() * 1000).toString());
        this.subPlan = [];
        this.resetStrategy();
    }
    showMoney(): [EnumBjlResult, number] {
        throw new Error("Method not implemented.");
    }
    resetRecm() {
        this.subPlan.forEach(item => item.resetRecm());
    }
    resetStrategy(): void {
        //根据自己记录胜负数投注，红色正向投注，蓝色反向投注，0不投，


        let flPlan = new ExcutePlan();
        flPlan.planName = "复利计划";
        flPlan.recmStrategy = [new FixedStrategy({ reverse: false })]
        flPlan.moneyStrategy = [new NormalGroupMoneyStrategy("")];
        flPlan.toResult(new RoundInfo());

        this.subPlan = [flPlan];
        let array = [2];
        //this.subPlan = [flPlan];
        for (let i = 0; i < array.length; i++) {
            let iv = array[i];
            let gi = new ExcutePlan();
            gi.planName = `正向${iv}组`;
            gi.baseMoney = 200;
            gi.relationPindex = [this.subPlan.length + 1];
            gi.recmStrategy = [new FixedStrategy({ reverse: false })];
            gi.moneyStrategy = [new GroupStepAddMoneyStrategy(`$wisdom-group-${i}`, {
                winGroupCount: (iv),
                failGroupCount: (iv),
                addStep: 200,
                devideStep: -200,
                reverse: false, //true:赢了+100，输了-100
                upLimit: 10000,
                baseMoney: gi.baseMoney
            })]
            gi.toResult(new RoundInfo());
            this.subPlan.push(gi);

            let gif = new ExcutePlan();
            gif.planName = `反向${iv}组`;
            gif.baseMoney = 200;
            gif.relationPindex = [this.subPlan.length - 1];
            gif.recmStrategy = [new FixedStrategy({ reverse: true })];
            gif.moneyStrategy = [new GroupStepAddMoneyStrategy(`$reverse-wisdom-group-${i}`, {
                winGroupCount: (iv),
                failGroupCount: (iv),
                addStep: 200,
                devideStep: -200,
                reverse: false, //true:赢了+100，输了-100
                upLimit: 10000,
                baseMoney: gif.baseMoney
            })]
            gif.toResult(new RoundInfo());
            this.subPlan.push(gif);


        }

    }
    toMoney(roundInfo: RoundInfo, recmInfo: RecmInfo, userRecm: RecmInfo, baseMoney: number): [EnumBjlResult, number] {
        let recmV = EnumBjlResult.U;
        let recmMoney = 0;

        let sumWin = 0;
        this.subPlan.forEach((pl, index) => {
            pl.setResult(roundInfo, pl.recmInfo, pl.multiple, pl.waterRate, pl.fuliRate);
            let plan = pl as ExcutePlan;


            let userRecmV = plan.toResult(roundInfo);
            let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
            pl.userRecmInfo.recmV = userRecmV;

            let [rv, rm] = plan.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
            pl.recmInfo.preBetCount = pl.recmInfo.betCount;
            pl.recmInfo.recmV = rv;
            pl.recmInfo.recmMoney = rm;

            let win = pl.recmInfo.summaryInfo.winMoney - pl.recmInfo.summaryInfo.failMoney;
            sumWin += win;

            if (index > 0) {
                let gstrategy = pl.moneyStrategy[0] as GroupStepAddMoneyStrategy;
                if (gstrategy.isUpLevel()) {
                    plan.relationPindex?.forEach((rpi) => {
                        var relationStrategy = this.subPlan[rpi].moneyStrategy[0] as GroupStepAddMoneyStrategy;
                        this.subPlan[rpi].recmInfo.recmV = EnumBjlResult.U;
                        relationStrategy.stop();
                    })
                }
            }

        });


        if (sumWin >= 300 || userRecm.summaryInfo.winCount == userRecm.summaryInfo.failCount) {
            this.subPlan.forEach((pl, index) => {
                let plan = pl as ExcutePlan;
                if (plan) {
                    plan.resetPlan();
                    let userRecmV = plan.toResult(roundInfo);
                    let newRoundInfo: RoundInfo = Object.assign({}, roundInfo, { userRecm: userRecmV });
                    pl.userRecmInfo.recmV = userRecmV;

                    let [rv, rm] = plan.toMoney(newRoundInfo, recmInfo, userRecm, pl.baseMoney);
                    pl.recmInfo.preBetCount = pl.recmInfo.betCount;
                    pl.recmInfo.recmV = rv;
                    pl.recmInfo.recmMoney = rm;
                }
            });
        }

        let zm = 0;
        let xm = 0;
        this.subPlan.forEach((pl, index) => {

            if (pl.recmInfo.recmV == EnumBjlResult.Z) {
                zm += pl.recmInfo.recmMoney;
                pl.recmInfo.betCount++;

            }
            else if (pl.recmInfo.recmV == EnumBjlResult.X) {
                xm += pl.recmInfo.recmMoney;
                pl.recmInfo.betCount++;
            }
        });
        if (zm > xm) {
            recmV = EnumBjlResult.Z;
            recmMoney = zm - xm;
        }
        else if (xm > zm) {
            recmV = EnumBjlResult.X;
            recmMoney = xm - zm;
        }
        else {
            recmV = EnumBjlResult.U;
        }

        return [recmV, recmMoney];
    }

    copyValue(valueObj: any): void {
        var obj = valueObj as WisdomMoneyStrategyV4;
        if (obj) {
            this.debugCompoenent = obj.debugCompoenent;
            this.startIndex = obj.startIndex;
            for (let i = 0; i < this.subPlan.length; i++) {
                this.subPlan[i].copyValue(obj.subPlan[i]);
            }
        }
    }
}